retour\arch\x86/
meta.rs

1use super::thunk;
2use crate::{error::Result, pic};
3use std::mem;
4
5/// The furthest distance between a target and its detour (2 GiB).
6pub const DETOUR_RANGE: usize = 0x8000_0000;
7
8/// Returns the preferred prolog size for the target.
9pub fn prolog_margin(_target: *const ()) -> usize {
10  mem::size_of::<thunk::x86::JumpRel>()
11}
12
13/// Creates a relay; required for destinations further away than 2GB (on x64).
14pub fn relay_builder(target: *const (), detour: *const ()) -> Result<Option<pic::CodeEmitter>> {
15  let displacement = (target as isize).wrapping_sub(detour as isize);
16
17  if cfg!(target_arch = "x86_64") && !crate::arch::is_within_range(displacement) {
18    let mut emitter = pic::CodeEmitter::new();
19    emitter.add_thunk(thunk::jmp(detour as usize));
20    Ok(Some(emitter))
21  } else {
22    Ok(None)
23  }
24}